package com.example.recyclerviewdemo.demo;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.recyclerviewdemo.R;

import java.util.List;

public class ChatDemoAdapter extends RecyclerView.Adapter<ChatDemoAdapter.ViewHolder> {
    private List<Msg> mChatList;

    public ChatDemoAdapter(List<Msg> mChatList) {
        this.mChatList = mChatList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mLeftChat;
        private TextView mRightChat;
        private LinearLayout mllLeftMsg;
        private LinearLayout mllRightMsg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mllLeftMsg = itemView.findViewById(R.id.ll_left_msg);
            mllRightMsg = itemView.findViewById(R.id.ll_right_msg);
            mLeftChat = itemView.findViewById(R.id.tv_chat_left);
            mRightChat = itemView.findViewById(R.id.tv_chat_right);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_demo_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Msg msg = mChatList.get(i);
        if (msg.getType() == Msg.TYPE_RECEIVED) {
            //说明收到的是左边的信息
            viewHolder.mllLeftMsg.setVisibility(View.VISIBLE);
            viewHolder.mLeftChat.setText(msg.getContent());
            viewHolder.mllRightMsg.setVisibility(View.GONE);
        } else if (msg.getType() == Msg.TYPE_SEND) {
            //说明收到的是右边的
            viewHolder.mllRightMsg.setVisibility(View.VISIBLE);
            viewHolder.mRightChat.setText(msg.getContent());
            viewHolder.mllLeftMsg.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mChatList.size();
    }


}
